<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<div th:insert="/common/common.html"></div>
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
</div>
</body>

</html>
<SCRIPT type="text/javascript">
    <!--
    var setting = {
        check: {
            enable: true,
            autoCheckTrigger: true
        },
        async: {
            enable: true,
            dataType: "json",
            url: "/menu/getInfoZtree",
            autoParam: ["id","name"]
        },
        view : {
            addHoverDom: function(treeId, treeNode){
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                var s = '<span id="btnGroup'+treeNode.tId+'">';
                if (treeNode.children == undefined) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                }else{
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                }
                s += '</span>';
                aObj.after(s);
            },
            removeHoverDom: function(treeId, treeNode){
                $("#btnGroup"+treeNode.tId).remove();
            }
        }
    };

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
    });
    //-->
    //增加
    function addNode(mid) {
        BootstrapDialog.show({
            title: '增加',
            message: $('<div></div>').load('/menu/toAdd'),
            closeable: true,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                action: function (dialog) {
                    $.ajax({
                        url: "/menu/add",
                        type: "post",
                        dateType: "json",
                        data: {"text": $("#menuName").val(),"url": $("#menuUrl").val(),"parentid": mid},
                        success : function(result){
                            toastr.success("增加成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);
                        },
                        error : function(data){
                            console.log(data)
                            toastr.error("网络异常");
                        }
                    })
                }
            }]
        })
    }
    //修改
    function editNode(mid) {
        BootstrapDialog.show({
            title: '修改',
            message: $('<div></div>').load('/menu/toUpdate/' + mid),
            closeable: true,
            buttons: [{
                label: '取消',
                action: function (dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                action: function (dialog) {
                    $.ajax({
                        url: "/menu/update",
                        type: "post",
                        dateType: "json",
                        data: $("#update_from").serialize(),
                        success : function(result){
                            toastr.success("修改成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);
                        },
                        error : function(data){
                            console.log(data)
                            toastr.error("网络异常");
                        }
                    })
                }
            }]
        })
    }
    //删除
    function deleteNode(mid) {
        if(confirm("确认删除吗?")) {
            $.ajax({
            url: "/menu/deleteById/" + mid,
            type: "post",
            dateType: "json",
            success : function(result){
                toastr.success("删除成功");
                $.fn.zTree.init($("#treeDemo"), setting);
            },
            error : function(data){
                console.log(data)
                toastr.error("网络异常");
            }
        })
        }
    }
</SCRIPT>